package com.donggua.aichebao.commonui.load;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

/**
 * Created by fzh on 2017/8/22.
 */
// com.donggua.aichebao.commonui.load.TextAndEditLayout
public class TextAndEditLayout extends LinearLayout{
    private float density = 1;
    private TextView tvLeft;
    private EditText etRight;
    private final static int DEFAULT_SIZE = 15;
    private final static int DEFAULT_COLOR = 0xff000000;
    public TextAndEditLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOrientation(LinearLayout.HORIZONTAL);
        this.setGravity(Gravity.CENTER_VERTICAL);
        density = context.getResources().getDisplayMetrics().density;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextAndEdit_Layout);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        String text = ta.getString(R.styleable.TextAndEdit_Layout_TextAndEdit_Text);
        int textColor = ta.getColor(R.styleable.TextAndEdit_Layout_TextAndEdit_TextColor,DEFAULT_COLOR);
        int textWidth = (int)ta.getDimension(R.styleable.TextAndEdit_Layout_TextAndEdit_TextWidth,-1);
        int textSize = (int)ta.getDimension(R.styleable.TextAndEdit_Layout_TextAndEdit_TextSize,DEFAULT_SIZE);
        if(textWidth > 0){
            layoutParams.width = textWidth;
        }
        layoutParams.rightMargin = dpToPx(5);
        tvLeft = new TextView(context);
        tvLeft.setGravity(Gravity.RIGHT);
        tvLeft.setText(text);
        tvLeft.setTextColor(textColor);
        tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
        tvLeft.setLayoutParams(layoutParams);
        this.addView(tvLeft);

        etRight = new EditText(context);
        layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        textColor = ta.getColor(R.styleable.TextAndEdit_Layout_TextAndEdit_Edit_TextColor,DEFAULT_COLOR);
        textSize = (int)ta.getDimension(R.styleable.TextAndEdit_Layout_TextAndEdit_Edit_TextSize,DEFAULT_SIZE);
        Log.e("text","textSize is "+textSize);
        Drawable bk = ta.getDrawable(R.styleable.TextAndEdit_Layout_TextAndEdit_Edit_BK);
        etRight.setTextColor(textColor);
        etRight.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
        if(bk != null){
            etRight.setBackgroundDrawable(bk);
        }
        etRight.setLayoutParams(layoutParams);
        this.addView(etRight);
        ta.recycle();
    }

    public int dpToPx(int dp){
        int px = (int)(dp*density+0.5);
        return px;
    }

    public String getInputText(){
        return etRight.getText().toString();
    }
}
